<template>
    <div>
        <div>
            <div>
                <label for="">
                    账号：
                    <input type="text" v-model="name" />
                </label>
            </div>
            <div>
                <label for="">
                    密码：
                    <input type="text" v-model="password" />
                </label>
            </div>
            <div>
                <label for="">
                    确认密码：
                    <input type="text" v-model="ckPassword" />
                </label>
            </div>
            <button @click="registered">注册</button>
            <button @click="toLogin">去登录</button>
        </div>
    </div>
</template>

<script>
import network from '../network/index';
export default {
    data() {
        return {
            name: '',
            password: '',
            ckPassword: ''
        };
    },
    methods: {
        registered() {
            if (!this.name) {
                alert('名称不能为空');
            } else if (!this.password) {
                alert('密码不能为空');
            } else if (this.password !== this.ckPassword) {
                alert('两次密码不相同');
            } else {
                network({
                    url: '/registered',
                    method: 'post',
                    data: {
                        name: this.name,
                        password: this.password
                    }
                }).then((res) => {
                    if (res.data.code === 200) {
                        this.$message('注册成功');
                        this.$router.push('/login');
                    } else {
                        this.$message(res.data.data);
                    }
                });
            }
        },
        toLogin() {
            this.$router.push('/login');
        }
    }
};
</script>

<style></style>
